<?php

class Lienket extends MY_Admin
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();
			
		// Check if the logged user is an administrator
		//$this->access_library->check_access();

		// Load needed models, libraries, helpers and language files 
		$this->load->module_model('admin', 'lienket_model', 'lienket');
		
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'lienket');
	}

	// Public methods
	public function exchange()
	{
		$id = $this->input->post('id');
		if($id !=null)
		{
			$link = $this->links->get_link($id);
			$status = 0;
			if($link['status']=='0')
			{
				$status = 1;
			}			
			$this->links->banner_exchange($id, $status); //link is the same banner;	
			$row['result'] = 1;
			$row['status'] = $status;
			echo json_encode($row);		
		}
		else
		{
			$row['result'] = 0;
			echo json_encode($row);
		}
	}
	public function index()
	{
		$lang_working = load_language_working();
		$data['links'] = $this->lienket->get_all_lienket();
		$data['title'] = 'Danh sách các Liên kết';
		$this->_template['page']	= 'website/lienket/list';

		$this->system_library->load($this->_template['page'], $data, TRUE);
	}

	public function add()
	{
		$lang_working = load_language_working();
		$data['title']	=	'Thêm Liên kết';
		$this->form_validation->set_rules('name', 'lang:form_name', 'required|max_length[50]|xss_clean');
		$this->form_validation->set_rules('url', 'lang:form_url', 'required|xss_clean');
		$this->form_validation->set_rules('target', 'lang:form_target', 'required|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'required|max_length[100]|xss_clean');
		$this->form_validation->set_rules('visible', 'lang:form_visible', 'required|xss_clean');
		
		if ($this->form_validation->run() == TRUE)
		{
			$this->lienket->add_lienket($lang_working['id']);
			
			$this->session->set_flashdata('message', lang('successfully_created'));

			redirect('admin/website/lienket', 'refresh');
		}
		$this->_template['page']	= 'website/lienket/add';		
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}

	public function edit($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		$data['title']	=	'Sửa Liên kết';
		$this->form_validation->set_rules('name', 'lang:form_name', 'required|max_length[50]|xss_clean');
		$this->form_validation->set_rules('url', 'lang:form_url', 'required|xss_clean');
		$this->form_validation->set_rules('target', 'lang:form_target', 'required|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'required|max_length[100]|xss_clean');
		$this->form_validation->set_rules('visible', 'lang:form_visible', 'required|xss_clean');

		$this->form_validation->set_error_delimiters('', '<br />');
			
		$data['link'] = $this->lienket->get_lienket($id);
			
		if ($this->form_validation->run() == TRUE)
		{
			$this->lienket->edit_lienket($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));

			redirect('admin/website/lienket', 'refresh');
		}
		
		$this->_template['page']	= 'website/lienket/edit';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->lienket->change_status_lienket($id,1);
		redirect('admin/website/lienket', 'refresh');
		
	}
	
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->lienket->change_status_lienket($id,0);
		redirect('admin/website/lienket', 'refresh');
	}
	
	public function delete($id)
	{
		$this->lienket->delete_lienket($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/website/lienket', 'refresh');
	}
	
}

/* End of file links.php */
/* Location: ./application/modules/admin/controllers/links.php */